<%@ Page Language="C#" Theme="Defaultsys" AutoEventWireup="true" CodeFile="BusinessRule.aspx.cs"
    Inherits="BusinessRule" Title="Rapport Plan System - Business rule" MasterPageFile="~/MasterPage.master" %>

<%@ MasterType VirtualPath="~/MasterPage.master" %>
<asp:Content runat="server" ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1">

    <script language="javascript" type="text/javascript">
	
		function TypeSelected2(oSrc, args)
		{
		    var objCombo = document.getElementById('ctl00_ContentPlaceHolder1_cmbType');
		    var objNr = document.getElementById('ctl00_ContentPlaceHolder1_txtDaysFromContact');
		    args.IsValid = true;     
		    if (objNr == null)
		    {
		        return true;
		    }      
            if (objCombo.value == 2 && objNr.value == "")
                args.IsValid = false;
                
            if (!isInteger(objNr.value))
            {
                args.IsValid = false;
            }
        }	
        
        function validateRange()
        {
          var s = document.frmInput.txtInput.value;
          var A = document.frmInput.txtA.value;
          var B = document.frmInput.txtB.value;

          switch (isIntegerInRange(s, A, B))
          {
             case true:
                alert(s + " is in range from " + A + " to " + B)
                break;
             case false:
                alert(s + " is not in range from " + A + " to " + B)
          }
        }

        // isIntegerInRange (STRING s, INTEGER a, INTEGER b)
        function isIntegerInRange (s, a, b)
        {   if (isEmpty(s))
             if (isIntegerInRange.arguments.length == 1) return false;
             else return (isIntegerInRange.arguments[1] == true);

          // Catch non-integer strings to avoid creating a NaN below,
          // which isn't available on JavaScript 1.0 for Windows.
          if (!isInteger(s, false)) return false;

          // Now, explicitly change the type to integer via parseInt
          // so that the comparison code below will work both on
          // JavaScript 1.2 (which typechecks in equality comparisons)
          // and JavaScript 1.1 and before (which doesn't).
          var num = parseInt (s);
          return ((num >= a) && (num <= b));
        }

        function isInteger (s)
        {
          var i;

          if (isEmpty(s))
          if (isInteger.arguments.length == 1) return 0;
          else return (isInteger.arguments[1] == true);

          for (i = 0; i < s.length; i++)
          {
             var c = s.charAt(i);

             if (!isDigit(c)) return false;
          }

          return true;
        }

        function isEmpty(s)
        {
          return ((s == null) || (s.length == 0))
        }

        function isDigit (c)
        {
          return ((c >= "0") && (c <= "9"))
        }
        
        function TypeSelected0Or1From(oSrc, args)
		{
		    var objCombo = document.getElementById('ctl00_ContentPlaceHolder1_cmbType');
		    var objFrom = document.getElementById('ctl00_ContentPlaceHolder1_txtFrom');
		    args.IsValid = true;           
		    if (objFrom == null)
		    {
		        return true;
		    }
            
            if ((objCombo.value == 0 || objCombo.value == 1) && objFrom.value == "")
                args.IsValid = false;
            else 
            {                
                if (!isInteger(objFrom.value))
                {
                    args.IsValid = false;
                }
            } 
               
        }	
        
        function TypeSelected0Or1To(oSrc, args)
		{
		    var objCombo = document.getElementById('ctl00_ContentPlaceHolder1_cmbType');
		    var objTo = document.getElementById('ctl00_ContentPlaceHolder1_txtTo');
		    args.IsValid = true;  
		    if (objTo == null)
		    {
		        return true;
		    }         
            if ((objCombo.value == 0 || objCombo.value == 1) && objTo.value == "")
                args.IsValid = false;
            else 
            {
                if (!isInteger(objTo.value))
                {
                    args.IsValid = false;
                }
            } 
        }	
        
        
    </script>

    <script type="text/javascript" src="jscripts/date/calendar.js"></script>

    <script type="text/javascript" src="jscripts/date/calendar-setup.js"></script>

    <table border="0" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td class="contTable">
                <table border="0" cellpadding="0" cellspacing="1">
                    <tr>
                        <td>
                            Rule name:</td>
                        <td>
                            <asp:TextBox ID="txtRuleName" runat="server" Width="310px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtRuleName"
                                CssClass="error" ErrorMessage="Rule name is required" SetFocusOnError="True"
                                Display="Dynamic">*</asp:RequiredFieldValidator>
                            <asp:Label ID="lblSteluta" runat="server" ForeColor="Red" Text="*" Visible="False"></asp:Label></td>
                    </tr>
                    <tr>
                        <td>
                            Type:</td>
                        <td>
                            <asp:DropDownList ID="cmbType" runat="server" Width="319px" OnSelectedIndexChanged="cmbType_SelectedIndexChanged"
                                AutoPostBack="True">
                                <asp:ListItem Value="0">call</asp:ListItem>
                                <asp:ListItem Value="1">letter</asp:ListItem>
                                <asp:ListItem Value="2">email</asp:ListItem>
                            </asp:DropDownList></td>
                    </tr>
                    <tr>
                        <td valign="top">
                            Description:</td>
                        <td>
                            <asp:TextBox ID="txtDescription" runat="server" Height="100px" TextMode="MultiLine"
                                Width="320px"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>
                            Group of contacts:</td>
                        <td>
                            <asp:DropDownList ID="cmbGroupContacts" runat="server" Width="322px">
                                <asp:ListItem>new contacts</asp:ListItem>
                                <asp:ListItem>buyers</asp:ListItem>
                            </asp:DropDownList></td>
                    </tr>
                    <tr id="trLetter1" runat="server" visible="true">
                        <td>
                            No of days from first visit:</td>
                        <td>
                            <asp:TextBox ID="txtFrom" runat="server" Width="70px"></asp:TextBox>&nbsp;
                            <asp:CustomValidator ID="CustomValidator2" runat="server" ClientValidationFunction="TypeSelected0Or1From"
                                Display="Dynamic" ErrorMessage="Date is required" SetFocusOnError="False">*</asp:CustomValidator><asp:Label
                                    ID="lblDateFrom" runat="server" ForeColor="Red" Text="*" Visible="False" Width="6px"></asp:Label></td>
                    </tr>
                    <tr id="trLetter2" runat="server" visible="true">
                        <td>
                            Deadline (no of days from first visit):</td>
                        <td>
                            <asp:TextBox ID="txtTo" runat="server" Width="70px"></asp:TextBox>&nbsp;
                            <asp:CustomValidator ID="CustomValidator3" runat="server" ClientValidationFunction="TypeSelected0Or1To"
                                Display="Dynamic" ErrorMessage="Date is required" SetFocusOnError="False">*</asp:CustomValidator><asp:Label
                                    ID="lblDateTo" runat="server" ForeColor="Red" Text="*" Visible="False" Width="13px"></asp:Label></td>
                    </tr>
                    <tr id="trMail" runat="server" visible="false">
                        <td>
                            No.of days from first visit:
                        </td>
                        <td>
                            <asp:TextBox ID="txtDaysFromContact" runat="server" Width="70px"></asp:TextBox>
                            <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="TypeSelected2"
                                Display="Dynamic" ErrorMessage="No.of days from first visit is required" SetFocusOnError="False">*</asp:CustomValidator>
                        </td>
                    </tr>
                    <tr runat="server" visible="false" id="trMail1">
                        <td>
                            Email subject:</td>
                        <td>
                            <asp:TextBox ID="txtEmailSubject" runat="server" Width="318px"></asp:TextBox></td>
                    </tr>
                    <tr runat="server" id="trCall">
                        <td valign="top">
                            Call reason:</td>
                        <td>
                            <asp:TextBox ID="txtCallReason" runat="server" Height="100px" TextMode="MultiLine"
                                Width="320px"></asp:TextBox></td>
                    </tr>
                    <tr runat="server" id="trTemplate" visible="false">
                        <td>
                            Template</td>
                        <td>
                            <asp:DropDownList ID="cmbTemplates" runat="server" Width="334px">
                                <asp:ListItem>Template 1</asp:ListItem>
                                <asp:ListItem>Template 2</asp:ListItem>
                                <asp:ListItem>Template 3</asp:ListItem>
                            </asp:DropDownList></td>
                    </tr>
                    <tr>
                        <td>
                            Active:
                        </td>
                        <td>
                            <asp:CheckBox ID="ckActive" runat="server" Checked="True" /></td>
                    </tr>
                </table>
                <asp:Button ID="btnSave" runat="server" OnClick="Save" Text="Save" />
                <asp:Button ID="btnCancel" runat="server" OnClick="Cancel" Text="Cancel" CausesValidation="false" />
                <br />
                <asp:Label ID="lblSavedBusinessRule" runat="server" ForeColor="#FF8000" Text="Business rule was saved"
                    Visible="False"></asp:Label>
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtRuleName"
                                CssClass="error" ErrorMessage="(*)Rule name is required" SetFocusOnError="True"
                                Display="Dynamic"></asp:RequiredFieldValidator>
                            <asp:Label ID="lblRuleNameExist" runat="server" ForeColor="Red" Text="(*)Rule name already exist"
                                Visible="False" Width="209px"></asp:Label></td>
                    </tr>
                    <tr>
                        <td>
                            <asp:CustomValidator ID="CustomValidator6" runat="server" ClientValidationFunction="TypeSelected2"
                                Display="Dynamic" ErrorMessage="(*)No. of days from first visit is required"
                                SetFocusOnError="False"></asp:CustomValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:CustomValidator ID="CustomValidator4" runat="server" ClientValidationFunction="TypeSelected0Or1From"
                                Display="Dynamic" ErrorMessage="(*)No. of days from first visit is required"
                                SetFocusOnError="False"></asp:CustomValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:CustomValidator ID="CustomValidator5" runat="server" ClientValidationFunction="TypeSelected0Or1To"
                                Display="Dynamic" ErrorMessage="(*)Deadline is required" SetFocusOnError="False"></asp:CustomValidator>
                        </td>
                    </tr>
                </table>
                <asp:Label ID="lblDateExpected" runat="server" ForeColor="Red" Text="(*)The time interval is not set correctly"
                    Width="235px" Visible="False"></asp:Label>
            </td>
        </tr>
    </table>
</asp:Content>
